package com.allen.library.utils;

public class IntUtil {
    public static byte[] Int2Bytes(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }
    public static int bytes2Int(byte[] bt) {
        byte[] b = new byte[4];
        b[3] = bt[0];
        b[2] = bt[1];
        b[1] = bt[2];
        b[0] = bt[3];
        int result = 0;
        for (int i = 0; i < b.length; i++) {
            int tmpVal = (b[i] << (8 * (3 - i)));
            switch (i) {
                case 0:
                    tmpVal = tmpVal & 0xFF000000;
                    break;
                case 1:
                    tmpVal = tmpVal & 0x00FF0000;
                    break;
                case 2:
                    tmpVal = tmpVal & 0x0000FF00;
                    break;
                case 3:
                    tmpVal = tmpVal & 0x000000FF;
                    break;
            }
            result = result | tmpVal;
        }
        return result;
    }
}
